using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Baz
{
    public class Animation
    {
        Rectangle[] frames;
        float _frame_length = 1f / 4f;
        float _timer = 0f;
        int _current_frame = 0;
        

        /// <summary>
        /// Getters and Setters for _frame_length
        /// </summary>
        public int FramesPerSecond
        {
            get { return (int)(1f / _frame_length); }
            set { _frame_length = 1f / (float)value; }
        }
        /// <summary>
        /// Getter for current frame of use 
        /// </summary>
        public Rectangle CurrentFrame
        {
            get { return frames[_current_frame]; }
        }
        /// <summary>
        ///  - Creates Rectangles for animating from a sprite sheet.
        ///  - Use xOffset and yOffset to designat different areas
        ///    of a sprite sheet used in animation.
        ///  - Use width and height to desginate the width and height
        ///    of the sprite area to be used.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="numFrames"></param>
        /// <param name="xOffset"></param>
        /// <param name="yOffset"></param>
        public Animation(int width, int height, int numFrames, int xOffset, int yOffset)
        {
            frames = new Rectangle[numFrames];
            int frameWidth = width / numFrames;
            for (int i = 0; i < numFrames; ++i)
            {
                frames[i] = new Rectangle(xOffset  + (frameWidth * i),
                                            yOffset,
                                            frameWidth,
                                            height);
            }
        }
        /// <summary>
        /// Method to update the sprites based on game time
        /// i.e. Walking animations
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            _timer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (_timer >= _frame_length)
            {
                _timer = 0f;
                _current_frame = (_current_frame + 1) % frames.Length;
            }
        }
        /// <summary>
        /// Resets the current frame to the beginning and
        /// the timer to 0.
        /// </summary>
        public void Reset()
        {
            _current_frame = 0;
            _timer = 0;
        }

    }

}
